//数组无重复插入链表
//2.执行交集
#include <stdio.h>
#include <stdlib.h>

typedef struct list
{
	int val;
	struct list *next;
};

// 不重复的插入到链表中
void insert(list *&l, int a[], int n)
{
	l = (list *)malloc(sizeof(list));
	l->next = NULL;
	l->val = 0x7fffffff;
	for (int i = 0; i < n; i++)
	{
		list *pre = l, *p = l->next;
		while (p && p->val > a[i])
		{
			pre = p;
			p = p->next;
		}
		if ((p && p->val != a[i]) || p == NULL)
		{
			list *node = (list *)malloc(sizeof(list));
			node->next = p;
			pre->next = node;
			node->val = a[i];
		}
	}
}
// 交集
void fun(list *l, int a[], int n)
{
	for (int i = 0; i < n; i++)
	{
		list *pre = l, *p = l->next;
		while (p && p->val > a[i])
		{
			pre = p;
			p = p->next;
		}
		if (p && p->val == a[i])
		{
			pre->next = p->next;
			free(p);
		}
		else
		{
			list *node = (list *)malloc(sizeof(list));
			node->next = p;
			pre->next = node;
			node->val = a[i];
		}
	}
}
int main()
{
	/*******************code*******************/
	list *l;
	int a[] = {1, 10, 1, 7, 3, 4, 5, 7, 3};
	insert(l, a, 9);
	list *p = l->next;
	while (p)
	{
		printf("%d ", p->val);
		p = p->next;
	}
	printf("\n");
	fun(l, a, 9);
	p = l->next;
	while (p)
	{
		printf("%d ", p->val);
		p = p->next;
	}
	/******************************************/
	fflush(stdin);

	printf("\n\n****************************\n");
	printf("Press Enter key to continue\n");
	getchar();
	return 0;
	/******************************************/
}
